package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

type YwdvipsRBody struct {
	Data       []models.Ywdvipbuysql `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

func Ywdvips(ctx *gin.Context) {
	request := ProcuresBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Ywdvipbuysql
	res := data_source.Db.Model(&models.Ywdvipbuy{}).Select("ywdvipbuy.*, user.nickname, user.phone").Joins("left join user on user.id = ywdvipbuy.uid").Where("ywdvipbuy.status = 2")

	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}

	res.Model(&models.Ywdvipbuy{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("ywdvipbuy.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := YwdvipsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
